﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Blog.IService;
using Blog.Service;
namespace Blog.ServiceContainer
{
    public class Container
    {
        public static IContainer container = null;
        public static T Resolve<T>()
        {
            try
            {
                Initialize();
            }
            catch (Exception ex)
            {
                throw new System.Exception("IOC实例化出错！" + ex.Message);
            }
            return container.Resolve<T>();
        }
        public static void Initialize()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<UserInfoService>().As<IUserInfoService>().InstancePerLifetimeScope();
            builder.RegisterType<AdminService>().As<IAdminService>().InstancePerLifetimeScope();
            builder.RegisterType<CommentService>().As<ICommentService>().InstancePerLifetimeScope();
            builder.RegisterType<UserInfoService>().As<IUserInfoService>().InstancePerLifetimeScope();
            builder.RegisterType<ArticleService>().As<IArticleService>().InstancePerLifetimeScope();
            builder.RegisterType<AttentService>().As<IAttentService>().InstancePerLifetimeScope();
            builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerLifetimeScope();
            builder.RegisterType<ParentService>().As<IParentService>().InstancePerLifetimeScope();
            builder.RegisterType<ProblemService>().As<IProblemService>().InstancePerLifetimeScope();
            builder.RegisterType<WorkOrderService>().As<IWorkOrderService>().InstancePerLifetimeScope();
            container =builder.Build();
        }
    }
}
